package com.my.cd.mediator;

import java.util.HashMap;
import java.util.Map;

/**
 * 具体中介者对象：智能家居中介者
 */
public class SmartHomeMediator implements Mediator {

    private Map<String, Worker> colleagueMap;//用来注册时工作者对象存储
    private Map<String, String> interMap;//内部标准化名称与外部工作者提供的名称的对应

    public SmartHomeMediator() {
        colleagueMap = new HashMap<>();
        interMap = new HashMap<>();
    }

    @Override
    public void register(String colleagueName, Worker worker) {
        colleagueMap.put(colleagueName, worker);
        if (worker instanceof Alarm) {
            interMap.put("Alarm", colleagueName);
        } else if (worker instanceof CoffeeMachine) {
            interMap.put("CoffeeMachine", colleagueName);
        } else if (worker instanceof TV) {
            interMap.put("TV", colleagueName);
        } else if (worker instanceof Curtains) {
            interMap.put("Curtains", colleagueName);
        }
    }

    @Override
    public void receiveMessage(String colleagueName, int stateChange) {
        if (colleagueMap.get(colleagueName) instanceof Alarm) {
            if (stateChange == 0) {
                ((CoffeeMachine) (colleagueMap.get(interMap.get("CoffeeMachine")))).startCoffee();
                ((TV) (colleagueMap.get(interMap.get("TV")))).startTv();
            } else if (stateChange == 1) {
                ((TV) (colleagueMap.get(interMap.get("TV")))).stopTv();
            }
        } else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) {
            ((Curtains) (colleagueMap.get(interMap.get("Curtains")))).upCurtains();
        } else if (colleagueMap.get(colleagueName) instanceof TV) {
            System.out.println("do something");
        } else if (colleagueMap.get(colleagueName) instanceof Curtains) {
            System.out.println("do something else");
        }
    }

    @Override
    public void sendMessage(String colleagueName, int state) {
        System.out.println("todo: i will send a message to some workers");
    }

}
